/**
 * 点赞、取消点赞
 */

const Router = require('koa-router')
const router = new Router()
router.prefix('/api/favor')
const { FavorValidator } = require('@validator/validator.js')
const { Favor } = require('@model/favor.js')
const { Auth } = require('@middlewares/auth.js')
const { SuccessModel } = require('@model/resModel.js')

router.post('/like', new Auth().controllerAuthApi, async (ctx, next) => {
  const v = await new FavorValidator().validate(ctx)
  const artId = v.get('body.artId')
  const type = v.get('body.type')
  await Favor.like(artId, type, ctx.auth.uid)
  const request = `${ctx.method} ${ctx.path}`
  ctx.body = new SuccessModel({
    msg: '点赞成功'
  }, request)
})

router.post('/dislike', new Auth().controllerAuthApi, async (ctx, next) => {
  const v = await new FavorValidator().validate(ctx)
  const artId = v.get('body.artId')
  const type = v.get('body.type')
  console.log('ctx.auth.uid', ctx.auth.uid);
  await Favor.dislike(artId, type, ctx.auth.uid)
  const request = `${ctx.method} ${ctx.path}`
  ctx.body = new SuccessModel({
    msg: '取消点赞成功'
  }, request)
})

module.exports = router